}
+static void
+ncomponent_convert_to_double (BablFormat *source_fmt,
+ BablImage *source,
+ char *source_buf,
+ char *source_double_buf,
+ int n)
+{
+ BablImage *src_img;
+ BablImage *dst_img;
+
+ src_img = (BablImage *) babl_image_new (
+ babl_component_from_id (BABL_LUMINANCE), NULL, 1, 0, NULL);
+ dst_img = (BablImage *) babl_image_new (
+ babl_component_from_id (BABL_LUMINANCE), NULL, 1, 0, NULL);
+
+ dst_img->type[0] = (BablType *) babl_type_from_id (BABL_DOUBLE);
+ dst_img->pitch[0] = (dst_img->type[0]->bits / 8);
+ dst_img->stride[0] = 0;
+
+ src_img->data[0] = source_buf;
+ src_img->type[0] = source_fmt->type[0];
+ src_img->pitch[0] = source_fmt->type[0]->bits / 8;
+ src_img->stride[0] = 0;
+
+ dst_img->data[0] = source_double_buf;
+
+ babl_process (
+ assert_conversion_find (src_img->type[0], dst_img->type[0]),
+ src_img, dst_img,
+ n * source_fmt->components);
+ babl_free (src_img);
+ babl_free (dst_img);
+}
+
+static void
+ncomponent_convert_from_double (BablFormat *destination_fmt,
+ char *destination_double_buf,
+ BablImage *destination,
+ char *destination_buf,
+ int n)
+{
+ BablImage *src_img;
+ BablImage *dst_img;
+
+ src_img = (BablImage *) babl_image_new (
+ babl_component_from_id (BABL_LUMINANCE), NULL, 1, 0, NULL);
+ dst_img = (BablImage *) babl_image_new (
+ babl_component_from_id (BABL_LUMINANCE), NULL, 1, 0, NULL);
+
+ src_img->type[0] = (BablType *) babl_type_from_id (BABL_DOUBLE);
+ src_img->pitch[0] = (src_img->type[0]->bits / 8);
+ src_img->stride[0] = 0;
+
+ dst_img->data[0] = destination_buf;
+ dst_img->type[0] = (BablType *) babl_type_from_id (BABL_DOUBLE);
+ dst_img->pitch[0] = destination_fmt->type[0]->bits/8;
+ dst_img->stride[0] = 0;
+
+ dst_img->type[0] = destination_fmt->type[0];
+ src_img->data[0] = destination_double_buf;
+
+ babl_process (
+ assert_conversion_find (src_img->type[0], dst_img->type[0]),
+ src_img, dst_img,
+ n * destination_fmt->components);
+
+ dst_img->data[0] += dst_img->type[0]->bits / 8;
+ babl_free (src_img);
+ babl_free (dst_img);
+}
+
+
+
static int
process_same_model (Babl *babl,
BablImage *source,
}
double_buf = babl_malloc (sizeof (double) * n *
- BABL (babl->fish.source)->format.model->components);
+ BABL (babl->fish.source)->format.components);
- convert_to_double (
- (BablFormat *) BABL (babl->fish.source),
- BABL_IS_BABL (source) ? source : NULL,
- BABL_IS_BABL (source) ? NULL : (char *) source,
- double_buf,
- n
- );
+ if (
+ (BABL (babl->fish.source)->format.components ==
+ BABL (babl->fish.destination)->format.components)
+ && (BABL (babl->fish.source)->format.model->components !=
+ BABL (babl->fish.source)->format.components))
+ {
+ /* FIXME: should recursively invoke babl and look up an appropriate fish
+ * for the conversion and multiply n by the number of components.
+ */
+ ncomponent_convert_to_double (
+ (BablFormat *) BABL (babl->fish.source),
+ BABL_IS_BABL (source) ? source : NULL,
+ BABL_IS_BABL (source) ? NULL : (char *) source,
+ double_buf,
+ n
+ );
- convert_from_double (
- (BablFormat *) BABL (babl->fish.destination),
- double_buf,
- BABL_IS_BABL (destination) ? destination : NULL,
- BABL_IS_BABL (destination) ? NULL : (char *) destination,
- n
- );
+ ncomponent_convert_from_double (
+ (BablFormat *) BABL (babl->fish.destination),
+ double_buf,
+ BABL_IS_BABL (destination) ? destination : NULL,
+ BABL_IS_BABL (destination) ? NULL : (char *) destination,
+ n
+ );
+ }
+ else
+ {
+ convert_to_double (
+ (BablFormat *) BABL (babl->fish.source),
+ BABL_IS_BABL (source) ? source : NULL,
+ BABL_IS_BABL (source) ? NULL : (char *) source,
+ double_buf,
+ n
+ );
+ convert_from_double (
+ (BablFormat *) BABL (babl->fish.destination),
+ double_buf,
+ BABL_IS_BABL (destination) ? destination : NULL,
+ BABL_IS_BABL (destination) ? NULL : (char *) destination,
+ n
+ );
+ }
babl_free (double_buf);
return 0;
}
Babl *rgba_image;
Babl *destination_image;
+
if (BABL (babl->fish.source)->format.model ==
BABL (babl->fish.destination)->format.model)
return process_same_model (babl, source, destination, n);
--- /dev/null
+/* babl - dynamically extendable universal pixel conversion library.
+ * Copyright (C) 2005, Øyvind Kolås.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "config.h"
+#include <math.h>
+#include "babl-internal.h"
+
+#define PIXELS 7
+#define COMPONENTS 2048
+#define TOLERANCE 0
+
+float source_buf [PIXELS * COMPONENTS] =
+{
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
+ /* the rest of the input buffer is nulls */
+};
+
+unsigned char reference_buf [PIXELS * COMPONENTS] =
+{
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ 26, 51, 77, 102, 128, 153, 26, 51, 77, 102, 128, 153,
+ /* the rest of the reference buffer is nulls */
+};
+
+unsigned char destination_buf [PIXELS * COMPONENTS];
+
+static int
+test (void)
+{
+ int components;
+ int OK = 1;
+
+ for (components = 1; components < 2048; components ++)
+ {
+ Babl *fish;
+ Babl *src_fmt;
+ Babl *dst_fmt;
+ int i;
+
+ src_fmt = babl_format_n (babl_type ("float"), components);
+ dst_fmt = babl_format_n (babl_type ("u8"), components);
+
+ fish = babl_fish (src_fmt, dst_fmt);
+
+ babl_process (fish, source_buf, destination_buf, PIXELS);
+
+ for (i = 0; i < PIXELS * components; i++)
+ {
+ if (abs (destination_buf[i] - reference_buf[i]) > TOLERANCE)
+ {
+ babl_log ("%i-components, pixel %i component %i is %i should be %i",
+ components, i / components, i % components, destination_buf[i], reference_buf[i]);
+ OK = 0;
+ }
+ }
+ }
+ if (!OK)
+ return -1;
+ return 0;
+}
+
+int
+main (int argc,
+ char **argv)
+{
+ babl_init ();
+ if (test ())
+ return -1;
+ babl_exit ();
+ return 0;
+}